/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __7148BB2B_F73E_41ac_BC68_B61312D98EE7__
#define __7148BB2B_F73E_41ac_BC68_B61312D98EE7__

#include "../tools/head.h"

//////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_common
{
    class misc_impl : public xos_stl::mem_item< xos::release_impl< misc_impl >, thread_lock >,
		public i_misc
    {
    public:
        typedef xos::release_impl< misc_impl > T;

    public:
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //
        static int get_xos_item_data( ITEM_DATA*& item_ptr );

    public:
        misc_impl();
        ~misc_impl();

    public:
        int init();
        int term();

    public:
        // 以指定进制显示数据内容 : 16, 10, 8
        int bin_to_text( char * lpszOutData, int nOutLen, int * pnOutLen, const unsigned char * lpszInData, int nInLen, int decimal );

        // i_misc method
        int DoDES( char * Out, const char * In, long datalen, const char * Key, int keylen, bool bEncode );

        unsigned short cal_crc16( const unsigned char * lpData, int nLen );

        int url_encode( const char * lpszSrc, int nSrcLen, char * lpszDst, int nCharNum );
        int url_decode( const char * lpszSrc, int nSrcLen, char * lpszDst, int nCharNum );

        int base64_encode( const char * lpszSrc, int nSrcLen, char * lpszDst, int nCharNum );
        int base64_decode( const char * lpszSrc, int nSrcLen, char * lpszDst, int nCharNum );

        // 
        // 计算分页的url
        // 
        int page_info( int nMaxPage, int nPageNum, int nCurPage, bool bEnable, char * lpszPageInfoUrl, int nBufSize, int * lpnInfoSize );// 

        // 字串操作
        // 
        const char * split_full_file( const char * lpszFullFile, char * lpszPath, int nSize, int * lpnSize );
        const char * ext( const char * lpszFile, xos_box::i_string * pStr );
        int parse_uri_to_property( const char * lpszUri, xos_box::i_int_property * pProp );
        // \t -> \\t, ' -> \\', " -> \\"
        int trans( const char * lpszSrc, char * lpszDst, int nLen, int * lpnLen );
        int begin_with( const char * lpszStr, const char * lpszSub );
        int end_with( const char * lpszStr, const char * lpszSub );
        int trim( char * lpszStr, const char * lpszTrims );
        int valid( const char * lpszStr, int nLen );

        // split
        int str_split( char* lpszUrl, const char seperator = '/' );

        // 
        // 拷贝文件
        // 
        int copy( const char * lpszSrcFile, const char * lpszDstFile );

		// 
		// 常用判断方法
		// 
		bool is_valid_email_addr( const char * lpszEmail );
		bool is_valid_id_card( const char * lpszIdCard );
        bool is_valid_phone( const char * lpszPhone );

        // 
        // double比较
        // 
        bool approximately_equal( double a, double b, double epsilon = 1e-8 );
        bool essentially_equal( double a, double b, double epsilon = 1e-8 );
        bool definitely_greater( double a, double b, double epsilon = 1e-8 );
        bool definitely_less( double a, double b, double epsilon = 1e-8 );
        int double_compare( double a, double b );

        // 
        // endian
        // 
        uint64_t to_small_endian_64( uint64_t value );
        uint64_t to_big_endian_64( uint64_t value );

        uint32_t to_small_endian_32( uint32_t value );
        uint32_t to_big_endian_32( uint32_t value );

        uint64_t to_small_endian_64( double value );
        uint64_t to_big_endian_64( double value );

        uint32_t to_small_endian_32( float value );
        uint32_t to_big_endian_32( float value );

        uint64_t swap_endian_64( uint64_t value );
        uint32_t swap_endian_32( uint32_t value );

        bool is_small_endian();

    };
} // xos_common

#endif // __7148BB2B_F73E_41ac_BC68_B61312D98EE7__
